﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeMakeSP.Platform.SharePoint.Events;
using Microsoft.SharePoint;
using System.Drawing;
using WeMakeSP.Platform.SharePoint.Configuration;
using System.Text;
using WeMakeSP.ImageResizer.Configuration;
using WeMakeSP.ImageResizer.Services.ServiceImplementation;

namespace WeMakeSP.ImageResizer.Events
{
    /// <summary>
    /// The item event receiver used in picture libraries.
    /// </summary>
    public class ImageResizerItemEventReceiver : SPExtendedItemEventReceiver
    {
        #region Public Override Methods

        /// <summary>
        /// Synchronous before event that occurs when a new item is added to its containing object.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPItemEventProperties"></see> object that represents properties of the event handler.</param>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
        }

        /// <summary>
        /// Asynchronous After event that occurs after a new item has been added to its containing object.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPItemEventProperties"></see> object that represents properties of the event handler.</param>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPWeb currentWeb = properties.OpenWeb())
                    {
                        ImageResizerService imageService = 
                            new ImageResizerService(currentWeb);

                        Guid listId = properties.ListId;

                        ImageResizerPictureLibraryConfiguration configuration =
                            imageService.GetPictureLibraryConfiguration(properties.ListId);

                        if (configuration.Schedule == ImageResizerSchedule.Immediate)
                        {
                            ImageResizerJob job =
                                new ImageResizerJob(currentWeb.Site.WebApplication);

                            Guid siteId = currentWeb.Site.ID;
                            Guid webId = currentWeb.ID;

                            Guid itemId = properties.ListItem.UniqueId;

                            // Add the image to the job.

                            job.AddImage(siteId, webId, listId, itemId);
                            
                            // Submit the job.

                            job.SubmitJob(
                                configuration.HoursDelay, 
                                configuration.MinutesDelay, 
                                configuration.SecondsDelay);
                        }
                    }

                });

            }
            catch(Exception ex)
            {
                properties.Cancel = true;
                properties.ErrorMessage = ex.Message;
                
            }
        }

        /// <summary>
        /// Synchronous before event that occurs when an existing item is changed, for example, when the user changes data in one or more fields.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPItemEventProperties"></see> object that represents properties of the event handler.</param>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        }

        /// <summary>
        /// Asynchronous after event that occurs after an existing item is changed, for example, when the user changes data in one or more fields.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPItemEventProperties"></see> object that represents properties of the event handler.</param>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPWeb currentWeb = properties.OpenWeb())
                    {
                        ImageResizerService imageService =
                            new ImageResizerService(currentWeb);

                        Guid listId = properties.ListId;

                        ImageResizerPictureLibraryConfiguration configuration =
                            imageService.GetPictureLibraryConfiguration(properties.ListId);

                        if (configuration.Schedule == ImageResizerSchedule.Immediate)
                        {
                            ImageResizerJob job =
                                new ImageResizerJob(currentWeb.Site.WebApplication);

                            Guid siteId = currentWeb.Site.ID;
                            Guid webId = currentWeb.ID;

                            Guid itemId = properties.ListItem.UniqueId;

                            // Add the image to the job.

                            job.AddImage(siteId, webId, listId, itemId);

                            // Submit the job.

                            job.SubmitJob(
                                configuration.HoursDelay,
                                configuration.MinutesDelay,
                                configuration.SecondsDelay);
                        }
                    }

                });

            }
            catch (Exception ex)
            {
                properties.Cancel = true;
                properties.ErrorMessage = ex.Message;

            }
        }

        #endregion
    }
}